package com.rulin.asp.business.errorcorrection.controller;

import java.util.Date;
import java.util.List;
import java.util.UUID;

import org.apache.commons.lang3.tuple.Pair;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rulin.asp.business.entity.ErrorQuestionParam;
import com.rulin.asp.business.entity.Question;
import com.rulin.asp.business.entity.RepeatQuestion;
import com.rulin.asp.business.errorcorrection.entity.ErrorCollectionQues;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesAttach;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesParam;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuesResultRow;
import com.rulin.asp.business.errorcorrection.entity.ErrorQuestion;
import com.rulin.asp.business.errorcorrection.service.ErrorCorrectionService;
import com.rulin.asp.business.log.entity.QuestionLog;
import com.rulin.asp.business.log.service.QuestionLogService;
import com.rulin.asp.business.service.DictionaryService;
import com.rulin.asp.sso.entity.UserUtils;
import com.rulin.asp.sso.service.UserService;
import com.rulin.asp.util.Consists;
import com.rulin.asp.util.Tools;

/**
 * 纠错题目列表页面
 * @author yangsy
 *
 */
@Controller
@RequestMapping("/errorQuesList")
public class ErrorQuesListController {
	
	@Autowired
	private DictionaryService dictionaryService;
	
	@Autowired
	private ErrorCorrectionService errorCorrectionService;
	
	@Autowired
	private UserService userService;
	
	@Autowired
	private QuestionLogService questionLogService;
	
	@RequestMapping(value = "/quesListPage.html", method = RequestMethod.GET)
	public String quesListPage(Model model){
		setDiv(model);
		model.addAttribute("users", userService.getAllTeachers());
		return "business/errorcorrection/queslist";
	}
	
	@RequestMapping(value = "/quesListTeachPage.html", method = RequestMethod.GET)
	public String quesListTeachPage(Model model){
		setDiv(model);
		model.addAttribute("users", userService.getAllTeachers());
		return "business/errorcorrection/queslist_teach";
	}
	
	@RequestMapping(value = "/getList.json", method = RequestMethod.GET)
	@ResponseBody
	public String list(ErrorQuesParam param){
		Pair<Integer, List<ErrorQuesResultRow>> pair = errorCorrectionService.geErrortQuesListByParam(param);
		return Tools.toEasyuiDataGrid(pair.getLeft(), pair.getRight());
	}
	
	/**
	 * 获取编辑页面
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openEditPage.html", method = RequestMethod.GET)
	public String openEditPage(Model model,String quesId){
		setDiv(model);
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		model.addAttribute("repeatQuestions", repeatQuestions);
		model.addAttribute("form", errorQuestion);
		return "business/errorcorrection/ques-edit";
	}

	private List<RepeatQuestion> getRepeatQuestion(ErrorQuestion errorQuestion) {
		
		ErrorQuestionParam param = new ErrorQuestionParam();
		param.setBookClass(errorQuestion.getQuestion().getBookClass());
		param.setBookSecClass(errorQuestion.getQuestion().getBookSecClass());
		param.setPageNum(errorQuestion.getQuestion().getPageNum());
		param.setQuesId(errorQuestion.getQuestion().getQuesId());
		List<RepeatQuestion> repeatQuestions = errorCorrectionService.getRepeatQuestions(param);
		return repeatQuestions;
	}
	
	/**
	 * 教师编辑页面----图书纠错
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openMyEditPage.html", method = RequestMethod.GET)
	public String openMyEditPage(Model model,String quesId){
		setDiv(model);
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		Question question = errorQuestion.getQuestion();
		if(question.getReplyId() == null || "".equals(question.getReplyId())){
			question.setReplyId(UUID.randomUUID().toString());
		}
		if(errorQuestion.getErrorQuesAttach() == null){
			model.addAttribute("isError", "");
		}else{
			model.addAttribute("isError", errorQuestion.getErrorQuesAttach().getIsError());
		}
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		model.addAttribute("repeatQuestions", repeatQuestions);
		model.addAttribute("form", question);
	
		return "business/errorcorrection/myerrorques-edit";
	}
	
	/**
	 * 保存回复信息
	 * @param question
	 */
	@RequestMapping(value = "/saveQuestion.json", method = RequestMethod.POST)
	public void saveQuesition(ErrorCollectionQues question){
		ErrorQuestion errorQuestion = new ErrorQuestion();
		errorQuestion.setQuestion(question);
		
		ErrorQuesAttach errorQuesAttach = new ErrorQuesAttach();
		errorQuesAttach.setQuesId(question.getQuesId());
		errorQuesAttach.setIsError(question.getIsError());
		errorQuestion.setErrorQuesAttach(errorQuesAttach);
		
		errorCorrectionService.saveErrorQues(errorQuestion);
		
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("3");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	@RequestMapping(value = "/reply.json", method = RequestMethod.POST)
	public void reply(ErrorCollectionQues question){
		
		ErrorQuestion errorQuestion = new ErrorQuestion();
		question.setQuesType(Consists.QUES_TYPE_4);
		question.setReplyTime(new Date());
		errorQuestion.setQuestion(question);
		
		ErrorQuesAttach errorQuesAttach = new ErrorQuesAttach();
		errorQuesAttach.setQuesId(question.getQuesId());
		errorQuesAttach.setIsError(question.getIsError());
		errorQuestion.setErrorQuesAttach(errorQuesAttach);
		
		errorCorrectionService.saveErrorQues(errorQuestion);
		
		//保存题目日志信息
		QuestionLog log = new QuestionLog();
		log.setQuesId(question.getQuesId());
		log.setOperateType("7");
		log.setUserAccount(UserUtils.getUser().getAccount());
		log.setUserName(UserUtils.getUser().getUserName());
		questionLogService.addQuestionLog(log);
	}
	
	/**
	 * 进入图书纠错发布页面
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openApprovalPage.html", method = RequestMethod.GET)
	public String openApprovalPage(Model model,String quesId){
		setDiv(model);
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		model.addAttribute("repeatQuestions", repeatQuestions);
		model.addAttribute("form", errorQuestion);
	
		return "business/errorcorrection/quescollection-approval";
	}
	
	/**
	 * 完成业务流程
	 * @param quesId
	 */
	@RequestMapping(value = "/complete.json", method = RequestMethod.POST)
	public void complete(String quesId){
		errorCorrectionService.complete(quesId);
	}
	
	/**
	 * 进入银成医考完成页面
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openCompletePage.html", method = RequestMethod.GET)
	public String openCompletePage(Model model,String quesId){
		setDiv(model);
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		model.addAttribute("repeatQuestions", repeatQuestions);
		model.addAttribute("form", errorQuestion);
	
		return "business/errorcorrection/quescollection-complete";
	}
	
	/**
	 * 问题收集只读页面
	 * @param model
	 * @param quesId
	 * @return
	 */
	@RequestMapping(value = "/openReadPage.html", method = RequestMethod.GET)
	public String openReadPage(Model model,String quesId){
		setDiv(model);
		ErrorQuestion errorQuestion = errorCorrectionService.getErrorQuestion(quesId);
		
		List<RepeatQuestion> repeatQuestions = getRepeatQuestion(errorQuestion);
		model.addAttribute("repeatQuestions", repeatQuestions);
		model.addAttribute("form", errorQuestion);
	
		return "business/errorcorrection/quescollection-read";
	}
	
	public void setDiv(Model model){
		model.addAttribute("quesTypes", dictionaryService.getByModel("ques_type_param"));
		model.addAttribute("quesDiffLevels", dictionaryService.getByModel("ques_diff_level"));
		
		model.addAttribute("questionSources", dictionaryService.getByModel("question_source"));
		model.addAttribute("quesBookClasses", dictionaryService.getByModel("ques_book_class"));
		model.addAttribute("quesBookClasses1", dictionaryService.getByModel("ques_book_class_1"));
		model.addAttribute("quesBookClasses2", dictionaryService.getByModel("ques_book_class_2"));
		model.addAttribute("quesBookClasses3", dictionaryService.getByModel("ques_book_class_3"));
		model.addAttribute("quesBookClasses4", dictionaryService.getByModel("ques_book_class_4"));
	}
}
